//
//  PTStuLifeServiceDemandDetailViewController.m
//  PlanTech
//  生活服务-需求详情页
//  Created by ruifeng song on 2018/7/11.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTStuLifeServiceDemandDetailViewController.h"
#import "PTStuLSDemandDetailContentCell.h"
#import "PTStuLSDemandDetailHeaderCell.h"
#import "PTCoreManager+LifeService.h"
#import "PTCoreObject+LifeService.h"
#import "PTStuLifeServiceMyPartJobViewController.h"
#import "SINACalendarUtils.h"

@interface PTStuLifeServiceDemandDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
    CGFloat footerHeight;
}
@property(nonatomic,strong)LifeServiceListDetailModel * detailModel;
@property (nonatomic,strong)UITableView *demandDetailTableView;

@end

@implementation PTStuLifeServiceDemandDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"需求详情";
    
    self.view.backgroundColor = BaseViewBackgroundColor;
    
    [self createView];
    
    [self requestData];
    
}

-(void)createView
{
    [self.view addSubview:self.demandDetailTableView];
}

-(void)requestData
{
    WeakSelf;
    [[PTCoreManager shareInstance]performGetLifeServiceListDetailsWithServiceUUID:self.serviceUUID userUUID:self.userUUID Completion:^(id  _Nullable retObject, NSError * _Nullable retError) {
       
        NSLog(@"%@",retObject);
        
        if (retObject) {
            weakSelf.detailModel = retObject;
            [weakSelf.demandDetailTableView reloadData];
        }
        
    }];
}

-(UITableView *)demandDetailTableView
{
    if (_demandDetailTableView ==nil) {
        
        _demandDetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(12, 10, ScreenWidth - 24, ScreenHeight - TOP_HEIGHT - 22 - BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _demandDetailTableView.delegate = self;
        _demandDetailTableView.dataSource = self;
        _demandDetailTableView.scrollEnabled = NO;
        _demandDetailTableView.layer.cornerRadius = 8.0;
        _demandDetailTableView.layer.masksToBounds = YES;
        _demandDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _demandDetailTableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            return 90;
            break;
        case 1:
        {
            NSString * str= self.detailModel.userAddress;
            CGFloat sizeHeight =[str heightForFont:PTFONT_13 width:ScreenWidth - 24 - 44 - 70];
            
            return (23 * 4) + 25 + sizeHeight;
        }
            break;
        case 2:
            return 67;
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        return 63;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==1) {
        
        UIView * aview = [[UIView alloc]initWithFrame:CGRectMake(12, 0, ScreenWidth - 24, 63)];
        
        UILabel * lal = [[UILabel alloc]initWithFrame:CGRectMake(22, 25, aview.width-44, 18)];
        lal.text = @"详细信息";
        lal.font = PTFONT_BOLD_18;
        lal.textColor = PTColor_2e2e2e;
        [aview addSubview:lal];
        
        return aview;
    }
    return [[UIView alloc]init];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        
        static NSString *DemandDetailHeaderCellIdentifier = @"DemandDetailHeaderCell";
        PTStuLSDemandDetailHeaderCell * demandDetailHeaderCell = [tableView dequeueReusableCellWithIdentifier:DemandDetailHeaderCellIdentifier];
        if (demandDetailHeaderCell == nil) {
            
            demandDetailHeaderCell = [[PTStuLSDemandDetailHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DemandDetailHeaderCellIdentifier];
            demandDetailHeaderCell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UILabel * lineLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(23, 89.5,ScreenWidth-46 - 24, 0.5)];
            lineLabel1.backgroundColor = PTColor_e0e0e0;
            [demandDetailHeaderCell.contentView addSubview:lineLabel1];
        }
        [demandDetailHeaderCell updateDemandDetailHeaderCellWith:self.detailModel];
        
        return demandDetailHeaderCell;
    }
    else if (indexPath.section ==1)
    {
        static NSString *DemandDetailContentCellIdentifier = @"DemandDetailContentCell";
        PTStuLSDemandDetailContentCell * demandDetailContentCell = [tableView dequeueReusableCellWithIdentifier:DemandDetailContentCellIdentifier];
        if (demandDetailContentCell == nil) {
            
            demandDetailContentCell = [[PTStuLSDemandDetailContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DemandDetailContentCellIdentifier];
            demandDetailContentCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [demandDetailContentCell updateDemandDetailContentCellWith:self.detailModel];
        return demandDetailContentCell;
    }
    else if (indexPath.section ==2)
    {
        
        static NSString *DemandDetailBottomCellIdentifier = @"DemandDetailBottomCell";
        UITableViewCell * demandDetailBottomCell = [tableView dequeueReusableCellWithIdentifier:DemandDetailBottomCellIdentifier];
        if (demandDetailBottomCell == nil) {
            
            demandDetailBottomCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DemandDetailBottomCellIdentifier];
            demandDetailBottomCell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UILabel * lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, 0, ScreenWidth-46 - 24, 0.5)];
            lineLabel.backgroundColor = PTColor_e0e0e0;
            [demandDetailBottomCell.contentView addSubview:lineLabel];
            
            UILabel * feeLabel = [[UILabel alloc]initWithFrame:CGRectMake(22, (67 -18)/2, ScreenWidth-44 - 24, 18)];
//            feeLabel.text = @"酬金：8RMB";
            feeLabel.tag = 222;
            feeLabel.font = PTFONT_BOLD_18;
            feeLabel.textColor = PTColor_2e2e2e;
            [demandDetailBottomCell.contentView addSubview:feeLabel];
            
            UILabel * lineLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(23, 66.5,ScreenWidth-46 - 24, 0.5)];
            lineLabel1.backgroundColor = PTColor_e0e0e0;
            [demandDetailBottomCell.contentView addSubview:lineLabel1];
        }
        
        UILabel * lbl = [demandDetailBottomCell.contentView viewWithTag:222];
        lbl.text = [NSString stringWithFormat:@"酬金：%@RMB",self.detailModel.reward];
        
        return demandDetailBottomCell;
    }
    else
    {
        return [[UITableViewCell alloc]init];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section ==2) {
    
        NSString * str=self.detailModel.userAddress;
        CGFloat sizeHeight =[str heightForFont:PTFONT_13 width:ScreenWidth - 24 - 44 - 70];
        
        footerHeight = ScreenHeight - TOP_HEIGHT - 10 -12-BOTTOM_HEIGHT- 67 - 90 - (23 * 4) - 63 -25 - sizeHeight ;
        
        return footerHeight;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section ==2) {
        
        UIView * aview = [[UIView alloc]initWithFrame:CGRectMake(12, 0, ScreenWidth - 24, footerHeight)];
        
        UIButton *receptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [receptBtn setTitle:@"我要承接" forState:UIControlStateNormal];
        [receptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        receptBtn.titleLabel.font = PTFONT_18;
        receptBtn.layer.cornerRadius = 20;
        receptBtn.layer.masksToBounds = YES;
        receptBtn.backgroundColor = PTColor_d0d0d0;
        receptBtn.tag = 111;
        [receptBtn addTarget:self action:@selector(onClickReceptBtn:) forControlEvents:UIControlEventTouchUpInside];
        [receptBtn setFrame:CGRectMake(22,footerHeight - 25 - 40 , aview.width - 44, 40)];
        [aview addSubview:receptBtn];
        
        //一种情况按钮不能点(需求是自己发布的）
        
        SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
        //结束时间超出当前时间（为已过期）
        
        if ( [[SINACalendarUtils currentTimeStr] doubleValue] > [self.detailModel.endTime doubleValue]) {
            
            receptBtn.backgroundColor = PTColor_d0d0d0;
            receptBtn.tag = 111;
        }
        else if ([self.detailModel.userUUID isEqualToString:userModel.userUid] || [self.detailModel.status integerValue]== PTLifeService_TakeOrderUnDoneStatus) {
            
            receptBtn.backgroundColor = PTColor_d0d0d0;
            receptBtn.tag = 111;
        }
        else
        {
            receptBtn.backgroundColor = PTColor_349ef7;
            receptBtn.tag = 222;
        }
        
        return aview;
    }
    
    return [[UIView alloc]init];
}

-(void)onClickReceptBtn:(UIButton *)sender
{
    if (sender.tag == 222) {
        
        UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:nil message:@"承接后不可取消哦！\n请确定后承接" delegate:self cancelButtonTitle:@"再看看" otherButtonTitles:@"承接", nil];
        alertView.tag = 11;
        [alertView show];

    }
}

#pragma mark - UIAlertViewDelegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==11 && buttonIndex ==1) {
        
        SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
        
        //调用我要承接接口
        WeakSelf;
        [[PTCoreManager shareInstance]performPostAcceptOrdersWithUserUUID:userModel.userUid serviceOwnerUUID:self.detailModel.userUUID serviceUUID:self.detailModel.serviceUUID type:[self.detailModel.type integerValue] Completion:^(PTMJsonResult * retObject, NSError * _Nullable retError) {
            
            if (retObject) {
                
                if ([retObject.code integerValue] == kPTCore_Response_Code) {
                    
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已承接" delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alertView.tag =1111;
                    [alertView show];
                    
                }
            }
            else
            {
                
            }
            
        }];
    }
    else if (alertView.tag == 1111 && buttonIndex ==0)
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:kPT_Global_Notification_RefreshLifeServiceList object:nil];
        
        PTStuLifeServiceMyPartJobViewController * lifeServiceMyPartJobViewController = [[PTStuLifeServiceMyPartJobViewController alloc]init];
        [lifeServiceMyPartJobViewController setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:lifeServiceMyPartJobViewController animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
